// 统一管理用户相关的接口
import request from "@/utils/request";
import type {
    loginResponseData,
    loginForm,
    userInfoResponseData,
    registerForm,
    registerResponseData,
    EditPasswordForm,
    EditInfoData,
} from "./type";
// 统一管理接口
enum API {
    LOGIN_URL = "/notNeed/login",
    USERINFO_URL = "/need/user/getUserInfo",
    REGISTER_URL = "/notNeed/register",
    VIEWUSER_URL = "/notNeed/user/viewUserInfo",
    GETLIST_URL = "/notNeed/user/getUserList",
    EDITPWD_URL = "/need/user/editPassword",
    UPDATEINFO_URL = "/need/user/updateUserInfo",
    UPLOADAVATAR_URL = "/need/user/uploadAvatar",
}

// 暴露请求函数
// 登录接口
export const reqLogin = (data: loginForm) => request.post<any, loginResponseData>(API.LOGIN_URL, data);
// 获取用户信息
export const reqUserInfo = () => request.get<any, userInfoResponseData>(API.USERINFO_URL);
// 注册接口
export const reqRegister = (data: registerForm) => request.post<any, registerResponseData>(API.REGISTER_URL, data);
// 查看用户信息的接口
export const reqViewUser = (id: number | string) => request.get<any, userInfoResponseData>(API.VIEWUSER_URL + `/${id}`);
// 获取用户列表
export const reqUserList = (data: searchParams, signal: AbortSignal = new AbortController().signal) => {
    return request.post<any, ListResponseData<User>>(API.GETLIST_URL, data, { signal });
};
// 修改用户密码
export const reqEditPassword = (data: EditPasswordForm) => request.post<any, ResponseData>(API.EDITPWD_URL, data);
// 修改用户信息
export const reqEditUserInfo = (data: EditInfoData) => request.post<any, ResponseData>(API.UPDATEINFO_URL, data);
// 上传用户头像
export const reqUploadAvatar = (data: FormData) => {
    return request.post<any, ResponseData<{ url: string; avatar: string }>>(API.UPLOADAVATAR_URL, data);
};
